package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.cache.vo.CatalogVO;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 攻略明细管理
 */
@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;


    /**
     * 页面列表回显
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyQuery qo){

        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page",page);
        return "Strategy/list";
    }

   @RequestMapping("input")
   public String input(Model model,String id){
        // 攻略数据回显
       if (StringUtils.hasLength(id)){
           Strategy strategy = strategyService.get(id);
           model.addAttribute("strategy",strategy);
       }

       // 攻略主题下拉框数据显示
       List<StrategyTheme> themes = strategyThemeService.list();
       model.addAttribute("themes",themes);

       // 攻略分类下拉框数据显示
        List <CatalogVO> catalogs = strategyService.groupList();
        model.addAttribute("catalogs",catalogs);
        return "strategy/input";
   }

    /**
     *添加和编辑
     * @param strategy
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

    @RequestMapping("changeState")
    @ResponseBody
    public Object changeState(int state,String id){

        strategyService.changeState(state,id);
        return JsonResult.success();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){

        strategyService.delete(id);
        return JsonResult.success();
    }

}
